import { message } from 'antd';
import { matchDict } from '@/utils/utils';
import { getList, getCustDetail, custUpdateSubmit } from '@/services/maindatamanage';

export default {
  state: {
    formFieldsValue: {},
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    detailList: {},
    valuesArr: [
      'uniCreditCode',
      'mdCustId',
    ]
  },
  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getList, payload);
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
        yield put({
          type: 'getList',
          payload: {
            data: {},
            currentPage,
            pageSize,
          },
        });
      }
    },
    // 客户信息详情数据
    *getCustDetail({ payload }, { call, put }) {
      const response = yield call(getCustDetail, payload);
      if (response.success) {
        yield put({
          type: 'updateModalData',
          payload: {
            detailList: response.data || {},
          },
        });
      } else {
        message.warning(`${response.message || '获取员工详情数据：服务端错误'}`);
      }
      return response;
    },
    // 客户信息 提交数据
    *updateSubmit({ payload }, { call }) {
      const response = yield call(custUpdateSubmit, payload);
      return response;
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount || '0', current: currentPage, pageSize },
        },
      };
    },
    // 更新state数据
    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 匹配字典
    matchName(
      state,
      {
        payload: { detailList, dictCode },
      }
    ) {
      const corpNature = matchDict(dictCode.CORP_NATURE, detailList.corpNature) || '--';
      const industryCode = matchDict(dictCode.INDST_CODE, detailList.industryCode) || '--';
      return {
        ...state,
        detailList: { ...detailList, corpNature, industryCode },
      };
    },
  },
};
